home *** CD-ROM | disk | FTP | other *** search
- /* TextPane.c */
- /*
- * TextPane
- * Superclass: CPane.
- * Copyright © 1991 Martin Minow. All Rights Reserved.
- * All use and non-commercial distribution permitted.
- * Set tabs every 4 bytes.
- *
- * This is a very simple text-only, output-only, display
- * pane. It intentionally does not support cut/paste,
- * scrolling, filing and printing.
- */
-
- #ifdef DOCUMENTATION
-
- title TextPane
- superclass CPane
- subclasses none
-
- usage
-
- #include "TextPane.h"
-
- void ITextPane(
- CView *anEnclosure,
- CBureaucrat *aSupervisor,
- short aWidth,
- short aHeight,
- short hEncl,
- short vEncl,
- SizingOption hSizing,
- SizingOption vSizing,
- Str255 fontName,
- short fontHeight
- );
- Initialization: the parameters are as described in
- the CPane documentation. fontName and fontHeight
- define the font to be used to display the data.
- This cannot be changed after initialization.
-
- void Dispose(void);
- Dispose of the TextPane and its contents.
-
- void SetText(
- StringPtr someText
- );
- Put a line of text on the screen.
- It rolls text up the display.
-
- author
- Martin Minow
-
- copyright
- Copyright © 1991 Martin Minow. All Rights Reserved.
- Non-commercial use and distribution permitted.
-
- #endif
-
- #include <CTextEnvirons.h>
- #include <TBUtilities.h>
- #include "TextPane.h"
-
- #define itsTextInfo ((CTextEnvirons *) itsEnvironment)
-
-
- /*
- * The TextPane acts like a "glass teletype" -- messages
- * appear at the bottom and are scrolled off the top
- * (never to appear again). Since the TextPane is private
- * and we know it will fill the window, the normal CPane
- * parameters can be constructed here rather than being
- * passed. If this were a general-purpose pane, the full
- * CPane parameters should be passed.
- */
- PRIVATE void
- TextPane::ITextPane(
- CView *anEnclosure,
- CBureaucrat *aSupervisor,
- short aWidth,
- short aHeight,
- short aHEncl,
- short aVEncl,
- SizingOption aHSizing,
- SizingOption aVSizing,
- Str255 fontName,
- short fontHeight
- )
- {
- register int i;
- TextInfoRec aTextInfo;
- register StringHandle *theStrings;
- FontInfo info;
- short fontNumber;
- Rect textBox;
-
- CPane::IPane(
- anEnclosure,
- aSupervisor,
- aWidth, aHeight,
- aHEncl, aVEncl,
- aHSizing, aVSizing
- );
- itsEnvironment = new(CTextEnvirons);
- itsTextInfo->ITextEnvirons();
- GetFontNumber(fontName, &fontNumber);
- /*
- * Make sure there's a drawing environment.
- */
- Prepare();
- TextFont(fontNumber);
- TextSize(fontHeight);
- GetFontInfo(&info);
- itsLineHeight = info.ascent
- + info.descent
- + info.leading;
- /*
- * Initialize the font stuff from the current port.
- */
- aTextInfo.fontNumber = thePort->txFont;
- aTextInfo.theStyle = thePort->txFace;
- aTextInfo.theSize = thePort->txSize;
- aTextInfo.theMode = thePort->txMode;
- itsTextInfo->SetTextInfo(&aTextInfo);
- /*
- * hOffset and vOffset define the initial position
- * of the pen when drawing text.
- */
- hOffset = info.widMax / 2;
- vOffset = info.ascent;
- /*
- * Determine the number of lines we will show.
- */
- GetAperture(&textBox);
- itsNLines = (textBox.bottom - textBox.top);
- itsNLines /= itsLineHeight;
- /*
- * Create a vector of strings. Note that this
- * limits the class -- the application cannot
- * change the font or window size after the window
- * has been created. A "real" application would
- * display data in a CPanorama and store the strings
- * in a CList subclass.
- */
- vectorHandle = (StringHandle **)
- NewHandle(itsNLines * sizeof (StringHandle));
- /*
- * Lock the vectorHandle while we initialize
- * the string vector. This prevents any C compiler
- * optimizations in the "theStrings[i] = ..."
- * statement from causing "memory moved" problems.
- */
- MoveHHi(vectorHandle);
- HLock(vectorHandle);
- theStrings = *vectorHandle;
- for (i = 0; i < itsNLines; i++)
- theStrings[i] = NewString((StringPtr) "\p");
- HUnlock(vectorHandle);
- }
-
- /*
- * Dispose of the TextPane by deleting the strings and
- * the string vector. This is called by TCL when the
- * enclosing window is disposed. (Note also that TCL
- * will dispose of the environment object, too.)
- */
- void
- TextPane::Dispose()
- {
- register int i;
- register StringHandle *theStrings;
-
- HLock(vectorHandle);
- theStrings = *vectorHandle;
- for (i = 0; i < itsNLines; i++)
- DisposHandle(theStrings[i]);
- DisposHandle(vectorHandle);
- inherited::Dispose();
- }
-
- /*
- * The SetText method adds text to the TextPane.
- */
- void
- TextPane::SetText(
- StringPtr someText
- )
- {
- register StringHandle *theStrings;
- StringHandle tempHandle;
- register int i;
-
- theStrings = (StringHandle *) *vectorHandle;
- /*
- * Save the top string's handle for recycling,
- * move the others "up" in the display,
- * insert the old top string at the bottom,
- * and set its contents to the supplied value.
- * Finally, update the display.
- * Note that we must not depend on the value
- * of theStrings after calling SetString or
- * ScrollPane as either may move memory.
- *
- *** * The following sequence may not move memory.
- */
- tempHandle = theStrings[0];
- for (i = 1; i < itsNLines; i++)
- theStrings[i - 1] = theStrings[i];
- theStrings[itsNLines - 1] = tempHandle;
- /*
- *** * The above sequence may not move memory.
- */
- SetString(tempHandle, someText);
- ScrollPane();
- }
-
- /*
- * This is the only function that accesses the text.
- */
- StringHandle
- TextPane::GetText(
- short whichItem
- )
- {
- return ((*vectorHandle)[whichItem]);
- }
-
-
- /*
- * Draw is called by the TCL when part of the TextPane
- * has been uncovered. Note that the actual drawing
- * is done by a DrawItem method -- this lets us have
- * one common routine for both Draw and ScrollPane.
- * This is overkill for this demo application, but useful
- * when the thing being drawn is complex.
- */
- void
- TextPane::Draw(
- Rect *area
- )
- {
- register int i;
-
- for (i = 0; i < itsNLines; i++)
- DrawItem(i);
- }
-
- /*
- * This method scrolls the text pane up one line and draws
- * the last line (which has just been inserted).
- */
- PRIVATE void
- TextPane::ScrollPane()
- {
- Rect theView;
- RgnHandle tempRgn;
-
- Prepare();
- tempRgn = NewRgn();
- GetAperture(&theView);
- ScrollRect(&theView, 0, -itsLineHeight, tempRgn);
- DrawItem(itsNLines - 1);
- ValidRgn(tempRgn);
- DisposeRgn(tempRgn);
- }
-
- /*
- * This is the only function that draws the data.
- */
- PRIVATE void
- TextPane::DrawItem(
- short whichItem
- )
- {
- register StringHandle theString;
- SignedByte saveHState;
-
- theString = GetText(whichItem);
- saveHState = HGetState(theString);
- HLock(theString);
- MoveTo(
- hOffset,
- vOffset + (whichItem * itsLineHeight)
- );
- DrawString(*theString);
- HSetState(theString, saveHState);
- }
-